﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PVFSLEAP
{
	class C_NODE
	{
		F_SETT	ST = new F_SETT();

		// [ General Inoformation ] ---------------------------------------------------------------
		public	int		NDID;			// Node ID
		public	int		POSX;			// Position X
		public	int		POSY;			// Posttion Y
		public	int		PRID;			// Parent ID
		public	int		HPCT;			// Hop Count
		public	double	ENGY;			// Node Energy
		public	float	COST;			// Cost from the base station to a node
		public	int		MGCT;			// Message Count
		public	int		CHLD;			// Child Number of Each Node
		public	bool	VRND;			// Verification Node

		public	bool	CPNF;
		public	int[]	CPND;
		public	bool	WORM;

        public  int     GNRP;           // Generating the Number of Report
		public	int		OFVI;
		public	int		OFRI;			// Occurance of False Report Injection Attack
		public	int		OWRM;			// Occurance of Wormhole Attack

		public	int		DFVI;			// Detection of False Vote Injection Attack
		public	int		DFRI;			// Detection of False Report Injection Attack
		public	int		DWRM;			// Detection of Wormhole Attack

		public	double FTNS;
		public	int[] GNTC;

		Random R = new Random( ( int )DateTime.Now.Millisecond );
		Random N = new Random( ( int )DateTime.Now.Millisecond + 1 );
		Random C = new Random( ( int )DateTime.Now.Millisecond % 99 );

		public C_NODE()
		{
			// [ General Inoformation ]
			NDID = -1;
			POSX = -1;
			POSY = -1;
			PRID = -1;
			HPCT = -1;
			ENGY = 0.0f;
			COST = -0.0f;
			MGCT = 0;
			CHLD = 0;
			VRND = false;

			CPND = new int[ST.OTVFV];
			GNTC = new int[3];
			CPNF = false;
			WORM = false;

            GNRP = 0;
			DFVI = 0;
			DFRI = 0;
			DWRM = 0;

			OFVI = 0;
			OFRI = 0;
			OWRM = 0;

			FTNS = 0.0f;

			initializeArray( CPND );
			initializeArray( GNTC );
		}

		public void initializeArray( int[] arr )
		{
			for ( int i = 0; i < arr.Length; i++ )
				arr[i] = 0;
		}

		public void initializeArray( double[] arr )
		{
			for ( int i = 0; i < arr.Length; i++ )
				arr[i] = 0;
		}

		public int randPosX( int s, int e )
		{
			//System.Threading.Thread.Sleep( 1 );
			return R.Next( s, e - 20 );
		}

		public int randPosY( int s, int e )
		{
			//System.Threading.Thread.Sleep( 1 );
			return R.Next( s, e - 20 );
		}

		public void setPosition( int ndid, int posx, int posy )
		{
			NDID = ndid;
			POSX = posx;
			POSY = posy;
		}

		public int getRandom( int s, int e)
		{
			System.Threading.Thread.Sleep( 1 );
			return N.Next( s, e );
		}

		public int getRandomChild( int s, int e )
		{
			System.Threading.Thread.Sleep( 1 );
			return C.Next( s, e );
		}

		public int makeCompromisedNodes( C_NODE[] SN, int ndid )
		{
			int i = 0;
			bool flag = false;

			int temp = getRandomChild( 0, ST.OCHLD - 1 );

			for ( i = 0; i < ST.OTVFV; i++ )
			{
				if ( SN[ndid].CPND[i] == temp )
				{
					flag = true;
					break;
				}
			}

			if ( flag == true)
			{
				if ( ST.OTVFV >= i +1 )
					return -1;

				SN[ndid].CPND[i+1] = temp;
			}
			else
				SN[ndid].CPND[0] = temp;

			return 0;
		}
	}
}
